import {
	HTTP_REQUEST_URL
} from '@/config/app.js'

function getUserProfile() {
	return new Promise((reslove) => {
		console.log('点击了')
		wx.getUserProfile({
			desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			success: (res) => {
				console.log(res)
				reslove(res.userInfo)
			},
			fail: (msg) => {
				console.log(msg)
			}
		})
	})
}

export default async function(phoneRes) {
	console.log('phoneRes', phoneRes)
	// const userInfo = await getUserProfile()
	const userInfo = {
		nickName: '微信用户'
	}

	wx.login({
		success: function(res) {
			console.log('登录', res)
			if (res.code) {
				let formData = {
					code: res.code,
					nickName: userInfo.nickName,
					encryptedData: phoneRes.encryptedData,
					iv: phoneRes.iv
				}
				console.log('formData',formData)
				uni.showLoading()
				wx.request({
					url: HTTP_REQUEST_URL + '/user/loginInfo',
					method: 'POST',
					data: {
						code: res.code,
						nickName: userInfo.nickName,
						encryptedData: phoneRes.encryptedData,
						iv: phoneRes.iv
					},
					success: function(res2) {
						console.log(res2)
						uni.hideLoading()
						const {
							user_id
						} = res2.data
						wx.setStorageSync('token', user_id )
						wx.setStorageSync('userInfo', {
							user_id
						})
						wx.setStorageSync('userId', user_id )
						uni.redirectTo({
							url:'/pages/index/index'
						})
					}
				})
			}
		},
	})

}
